package com.zhibang.config.security;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhibang.entity.Users;
import com.zhibang.mapper.UsersMapper;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;


/**
 * 用户认证实现类
 */
@Component
public class MyUserDetailsService implements UserDetailsService {
    @Resource
    UsersMapper userMapper;

    // 用户登录时 调用本方法 完成用户信息的查询
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Users> qw = new QueryWrapper();
        qw.eq("username",username);
        Users user = userMapper.selectOne(qw);
        if(user == null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        return user;
    }
}
